﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sinacor.OMM.Util
{
    public static class MessageHelper
    {
        public static bool IsMessageIdGreaterThanLast(string currentMessageId, string lastMessageId)
        {
            List<string> messageIds = new List<string>();
            bool isGreater = false;

            messageIds.Add(currentMessageId);
            messageIds.Add(lastMessageId);

            if (string.IsNullOrEmpty(lastMessageId))
            {
                isGreater = true;
            }
            else
            {
                byte minorSequence = (from m in messageIds
                                      orderby
                                      Convert.ToByte(m.Substring(8, 1)[0])
                                      select Convert.ToByte(m.Substring(8, 1)[0])).First();

                string minorMessageId = (from m in messageIds
                                         where Convert.ToByte(m.Substring(8, 1)[0]) == minorSequence
                                         orderby Convert.ToInt32(m.Substring(9, 10))
                                         select m).First();

                if (minorMessageId == currentMessageId)
                {
                    isGreater = false;
                }
                else
                {
                    isGreater = true;
                }
            }

            return isGreater;
        }
    }
}
